package com.tdcoding.chat.helper;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TimerHelper {
	private static TimerHelper timerHelper;
	private ScheduledThreadPoolExecutor poolExecutor;  
	public static TimerHelper getInstance(){
		if (timerHelper==null) {
			timerHelper=new TimerHelper();
		}
		return timerHelper;
	}
	private TimerHelper(){
		int cpu =  Runtime.getRuntime().availableProcessors()*100;
		poolExecutor = new ScheduledThreadPoolExecutor(cpu);
	}
	
	public void scheduleAtFixedRate(Runnable runnable){
		poolExecutor.scheduleAtFixedRate(runnable, 1000, 1000, TimeUnit.SECONDS);
	}
	
	public void scheduleAtFixedRate(Runnable runnable,long initialDelay, long period){
		poolExecutor.scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.SECONDS);
	}
	
	public void scheduleAtFixedRate(Runnable runnable,long initialDelay, long period, TimeUnit unit){
		poolExecutor.scheduleAtFixedRate(runnable, initialDelay, period, unit);
	}
	
	public boolean cancelTasker(Runnable tasker){
		return poolExecutor.remove(tasker);
	}
	
	public void shutdown(){
		if (poolExecutor!=null) {
			poolExecutor.shutdown();
		}
	}
}
